#include <vector>
#include "convertor.h"
#include "libraw/libraw.h"
#include <boost/bind.hpp>
#include <boost/function.hpp>

const char imagePath[] = "/home/pigeon/images/";
#define T   iProcessor.imgdata.thumbnail



void CConvertor::convertFiles(/*const*/ CPrtVectorOfString files)
{
#if 0
    std::cout<<"Counts "<<files->size()<<"\n";
    CVectorOfString::const_iterator dit = files->begin();
    for(;dit!=files->end();++dit)
    {
        std::cout<<*dit<<"\n";
    }
#else    
    std::vector<tstring>::const_iterator it = files->begin();
    int nCount = files->size();
    LibRaw iProcessor;
    for(;it!=files->end();++it)
    {
        // Open the file and read the metadata
        int nRes = iProcessor.open_file((*it).c_str());
        nRes = iProcessor.unpack();
        nRes = iProcessor.unpack_thumb(); 
        char thumbfn[1024];
        /*snprintf*/_snprintf(thumbfn,sizeof(thumbfn),"%s/%s.jpg",CFileList::output_path.c_str(),CFileList::getFileName((*it)).c_str());
        printf("\n%s\n%s\n",(*it).c_str(),thumbfn);
        iProcessor.dcraw_thumb_writer(thumbfn);        
    }
    printf("\nthread finished job!!!\n");
#endif    
}

void CConvertor::Start(CFileList& fileList)
{
    const CPrtVectorOfVector flSource = fileList.getFileList();
    CVectorOfVector::const_iterator it = flSource->begin();
    int nCount = flSource->size();
    for(int i=0;it!=flSource->end();++it,++i)
    {
        m_ThreadPool[i] = boost::thread(&CConvertor::convertFiles,*it);
    }
}

CConvertor::~CConvertor()
{
    for(int i=0;i<numberOfWorkers;++i)
        m_ThreadPool[i].join();
}
